var express = require('express');
var router = express.Router();

var fs = require('fs')
var md5 = require('md5')
var mysql = require('mysql');
var client = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123',
    database:'blog',
    port: 3306
});
client.connect();
/* GET users listing. */
router.get('/login', function(req, res, next) {
  res.render('login');
});
//收银台 
//cookies + 文件 =session
//cookies + 数据库 = session
router.post('/dologin',function(req,res,next){
	req.body.password=md5(req.body.password)
	var sql ='select * from users where username="'+req.body.name+'" and password="'+req.body.password+'"';
	client.query(sql, function(err, rows, fields) {
		if (rows.length==0) {
			res.send('账户密码不正确')
		}else{
			req.session.username = req.body.name
			req.session.is_login = true
			res.send('ok')             
		}
	});
});

router.get('/sign', function(req, res, next) {
  res.render('sign');
});

router.post("/dosign",function(req,res,next){
	
	var sql = 'insert into users values(null,"'+req.body.username+'","'+req.body.password+'")'
	client.query(sql,function(err,rows,fields){
		res.redirect('/login')
	})
})





	/*if (req.body.username=='admin' && req.body.password=='123') {
		res.cookie("islogin","true")
		res.redirect('/admin')
	}else{
		res.send('账户密码不正确')
	}
	console.log()*/
/*})*/

module.exports = router;
